import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;

/**
 * 
 * @author Matthew Dye 
 * @version 1 6/17/11
 *
 */
public class Driver {
/**
 * Driver will read in information and create a text file containing the sorted data. Buffered Reader reads in data 
 * from the initial text file and converts it to a string array, which is in turn changed to a TestData array so comparisons can be determined.
 * @param args
 * 		File name of the data to be sorted and the method of sorting to use
 */
		public static void main(String[] args)
		{
			
			Sorter<TestData> sort = new Sorter<TestData>();
			String sortMethod = args[1];
			
			FileReader inputFil = null;
			File fil = new File(args[0]);
			try
			{
				inputFil = new FileReader(fil);
				BufferedReader in = new BufferedReader(inputFil);
				String length = in.readLine();
				
				ArrayList<String> lines = new ArrayList<String>();
				
		        String line = null;
		        while ((line = in.readLine()) != null) {
		            lines.add(line);
		        }
		        in.close();
		        
		        String[] ints = lines.toArray(new String[lines.size()]);
		        String numbers = ints[0];
		        StringTokenizer st = new StringTokenizer(numbers);
		        int i = 0;
		        TestData[] toBeSorted = new TestData[Integer.parseInt(length)];
		        while (st.hasMoreTokens())
		        {
		        	toBeSorted[i] = new TestData(Integer.parseInt(st.nextToken()));
		        	i++;
		        }
		    
			    
			    if(sortMethod.equalsIgnoreCase("selectionSort"))
			    {
			    	sort.selectionSort(toBeSorted);
			    	writeFile(args[0],toBeSorted,length);
			    	System.out.println((toBeSorted[0].getComparisons()) + " comparisons for " + length + " elements");
			    }
			    else if(sortMethod.equalsIgnoreCase("insertionsort"))
			    {
			    	sort.insertionSort(toBeSorted);
			    	writeFile(args[0],toBeSorted,length);
			    	System.out.println((toBeSorted[0].getComparisons()) + " comparisons for " + length + " elements");
			    }
			    else if(sortMethod.equalsIgnoreCase("mergesort"))
			    {
			    	sort.mergeSort(toBeSorted);
			    	writeFile(args[0],toBeSorted,length);
			    	System.out.println((toBeSorted[0].getComparisons()) + " comparisons for " + length + " elements");
			    }
			    else if(sortMethod.equalsIgnoreCase("quicksort"))
			    {
			    	sort.quickSort(toBeSorted);
			    	writeFile(args[0],toBeSorted,length);
			    	System.out.println((toBeSorted[0].getComparisons()) + " comparisons for " + length + " elements");
			    }
			    
			}
			
			
			
			catch(Exception e)
			{
				e.printStackTrace();
			}

			
			
			
		}
		/**
		 * writeFile is a helper method that creates a new file of sorted data, if one of the same name does not already exist
		 * @param fileName
		 * 		File name of the unsorted array
		 * @param array
		 * 		Array of sorted data
		 * @param length
		 * 		Length of data that has been sorted
		 */
		private static void writeFile(String fileName,TestData[] array,String length)
		{
			try
			{
			  StringTokenizer st = new StringTokenizer(fileName, ".");
			  String newFileName = st.nextToken() + "_sorted.txt";
			 
			  File file = new File(newFileName);
			  boolean exist = file.createNewFile();
			  if (!exist)
			  {
				  System.out.println("File already exists. Please delete the current file and try again");
				  System.exit(0);
			  }
			  else
			  {
				  FileWriter fstream = new FileWriter(newFileName);
				  BufferedWriter out = new BufferedWriter(fstream);
				  String toBeWritten = length + System.getProperty("line.separator");
				  for(int i = 0; i<array.length;i++)
					  toBeWritten = toBeWritten + array[i].toString() + " ";
				  
				  out.write(toBeWritten);
				  out.close();
				  System.out.println("File created successfully.");
			  }
			}
			catch(Exception e)
			{
				
			}
		}
}
